package stardust.core.network.protocol.http.support;


import java.util.ArrayList;
import java.util.List;


public class URI
{

  private String uri;

  private String[] path;

  public URI(String uri)
  {
    this.uri = uri.replace('\\', '/');

    /*
     * 提取路径。
     */
    if (this.uri.indexOf('?') == -1)
    {
      this.path = this.uri.split("/");
    }
    else
    {
      this.path = this.uri.substring(0, this.uri.indexOf('?')).split("/");
    }

    /*
     * 剔除空白路径。
     */
    List<String> pathList = new ArrayList<String>();

    for (int i = 0; i < this.path.length; i++)
    {
      if (!this.path[i].isEmpty())
      {
        pathList.add(this.path[i]);
      }
    }

    this.path = pathList.toArray(new String[0]);
  }

  public String[] getPath()
  {
    return this.path;
  }

  public String getURIString()
  {
    StringBuilder buffer = new StringBuilder();

    for (String s : this.path)
    {
      if (s.indexOf(':') == -1)
      {
        buffer.append('/');
      }
      
      buffer.append(s);
    }

    return buffer.length() == 0 ? "/" : buffer.toString();
  }

}
